package so.sao.shop.supplier.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import so.sao.shop.supplier.config.Constant;
import org.springframework.web.bind.annotation.*;
import so.sao.shop.supplier.domain.User;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.pojo.input.CheckOrderSearchInput;
import so.sao.shop.supplier.service.CheckingAccountResultService;
import so.sao.shop.supplier.util.JwtTokenUtil;
import so.sao.shop.supplier.util.Ognl;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>Version: supplier V1.2.0 </p>
 * <p>Title: CheckingAccountResultController</p>
 * <p>Description: 对账结果</p>
 *
 * @author: fangzhou
 * @Date: Created in 2017/11/22 10:29
 */
@RestController
@RequestMapping("/checkingResult")
public class CheckingAccountResultController {

    @Autowired
    private CheckingAccountResultService resultService;

    /**
     * 根据订单编号查询订单的支付记录及阿里对账数据的支付记录
     *
     * @param orderId 订单编号
     * @return
     */
    @GetMapping(value = "/selectPayment")
    public Result getPaymentByOrderId(@RequestParam(required = true)String orderId) {
        return resultService.findPaymentByOrderId(orderId);
    }

    /**
     * 多条件查询订单列表
     * @param checkOrderSearchInput
     * @return
     */
    @GetMapping(value = "/search",name = "根据条件查询订单列表")
    public Result search( CheckOrderSearchInput checkOrderSearchInput){
        return resultService.searchOrders(checkOrderSearchInput);
    }

    /**
     *根据对账类型查询对账结果
     * @param checkingType  对账类型
     * @param pageSize 当前页面大小(默认值10)
     * @param pageNum 当前页号(默认值1)
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/selectMatchingRecordByCheckType")
    public Result selectMatchingRecordByCheckType(@RequestParam Integer checkingType,@RequestParam Integer pageSize, @RequestParam Integer pageNum) throws Exception{
        //获取当前登录user
        User user = JwtTokenUtil.getCurrentUser();
        //判断是否登陆
        if (Ognl.isNull(user)) {
            return Result.fail(Constant.MessageConfig.MSG_USER_NOT_LOGIN);
        }
        return resultService.selectMatchingRecordByCheckType(checkingType,pageSize,pageNum,user.getId());
    }


    /**
     * 对账
     *
     * @return
     */
    @PostMapping(value = "/checking")
    public Result checkingAccount(HttpServletRequest request) throws IOException {
        // 获取登录人
        User user = JwtTokenUtil.getCurrentUser();
        if (Ognl.isNull(user)){
            return Result.fail("请先登录");
        }
        // 下载文件夹路径
        String tempPath = request.getSession().getServletContext().getRealPath("") + "/file";
        return resultService.checkingAccount(user.getId(),tempPath);
    }

}
